package org.itboys.core.controller.web;

import com.alibaba.fastjson.JSONObject;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.entity.mysql.SundayAddProductOrder;
import org.itboys.core.entity.mysql.SundayAddProductOrderSKU;
import org.itboys.core.service.mysql.web.SundayAddProductOrderService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/20
 * @Description：补货单
 */
@Controller
@RequestMapping("/sunday/web/order/addProduct")
public class SundayAddProductOrderController extends BaseController{
    @Autowired
    private SundayAddProductOrderService addProductOrderService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        return "/sunday/order/addProduct/index";
    }
    @RequestMapping("/input")
    public String input(HttpServletRequest request, Model model, @RequestParam("id") Long id){
        model.addAttribute("order",addProductOrderService.findOne(id));
        return "/sunday/order/addProduct/input";
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response)throws Exception{
        PageResult<SundayAddProductOrder> result = addProductOrderService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
    }
    @RequestMapping("/save")
    public void save(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam("id")Long id
            ,@RequestParam("preStoreId")Long preStoreId
            ,@RequestParam("centerStoreId")Long centerStoreId
            ,@RequestParam(value = "remark",required = false)String remark
            ,@RequestParam("skus")String skus)throws  Exception{
        List<SundayAddProductOrderSKU> sundayAddProductOrderSKUS = JSONObject.parseArray(skus, SundayAddProductOrderSKU.class);
            if(ListUtil.isNotNull(sundayAddProductOrderSKUS)){
                addProductOrderService.save(id,preStoreId,centerStoreId,remark,sundayAddProductOrderSKUS);
            }else {
                ResponseMessage.error(CommonConstants.FAIL_CODE,"请添加SKU！");
            }
            ResponseMessage.success();
    }

    @RequestMapping("/setState")
    public void setState(HttpServletRequest request,HttpServletResponse response
    ,@RequestParam("id")Long id
    ,@RequestParam("state")Integer state)throws Exception{
        addProductOrderService.setState(id,state);
        ResponseMessage.success();
    }
}
